<?php
$cs = Yii::app()->clientScript;

$class = get_class($model);
$script = "
var productNameTextBox = $('#" . $class . "_name');
productNameTextBox.click(function(){
    productNameTextBox.data('value', productNameTextBox.val());
});

productNameTextBox.blur(function(){
    var text = $(this).val();
    if (text == productNameTextBox.data('value')) return;
    var alias = $('#" . $class . "_alias').val();
    //if (alias.replace(/\s+/, '').length > 0) return false;
    var url = text
            .toLowerCase() // change everything to lowercase
            .replace(/^\\s+|\\s+$/g, '') // trim leading and trailing spaces
            .replace(/[_|\\s]+/g, '-') // change all spaces and underscores to a hyphen
            .replace(/[^a-z0-9-]+/g, '') // remove all non-alphanumeric characters except the hyphen
            .replace(/[-]+/g, '-') // replace multiple instances of the hyphen with a single instance
            .replace(/^-+|-+$/g, '') // trim leading and trailing hyphens
            ;
    $('#" . $class . "_alias').val(url);
});
";
$cs->registerScript('name-to-slug', $script, CClientScript::POS_READY);
?>

<div class="control-group">
    <?php echo $form->labelEx($model, 'name', array('class' => 'control-label',)); ?>
    <div class="controls">
        <?php echo $form->textField($model, 'name', array('size' => 60, 'maxlength' => 1500)); ?>
        <?php echo $form->error($model, 'name'); ?>
    </div>
</div>
<div class="control-group">
    <?php echo $form->labelEx($model, 'gallery', array('class' => 'control-label',)); ?>
    <div class="controls">
        <!--            --><?php //echo CHtml::image($model->image) ?>
        <?php
        $url = $this->createUrl('/Xpress/service/ajax');
        $this->widget('Gallery.components.widgets.GalleryManagerWidget', array(
            'model' => $model,
            'attribute' => 'gallery',
            'beforeDelete' => 'function(d,id){
    $.get(
        \'' . $url . '\',
        {
            SID : \'Shop.Product.usedGallery\',
            galleryId: id
        }
    ).done(function(res){
        res = eval(res);
        if (res.status != undefined && res.status == 0)
            d.resolve();
        else
        {
            alert("You can not delete gallery when it is used");
            d.reject();
        }
    }).fail(d.reject);
}'
        ));
        echo CHtml::link('<i class="icon-remove"></i>', '#', array('class' => 'unlink-gallery' . (empty($model->gallery) ? ' hide' : '')));
        $baseUrl = Yii::app()->assetManager->publish(Yii::getPathOfAlias('Gallery.components.widgets.*') . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'galleries');
        app()->clientScript->registerScript(__CLASS__ . 'UnlinkGallery', "$('.unlink-gallery').click(function(){
        $('#Product_gallery').val('');
        $('.choose-gallery img').attr('src','" . $baseUrl . "/images/no_image.jpg');
        $(this).addClass('hide');
        return false;
        });");
        ?>
    </div>
</div>
<div class="control-group">
    <?php echo $form->labelEx($model, 'alias', array('class' => 'control-label',)); ?>
    <div class="controls">
        <?php echo $form->textField($model, 'alias', array('size' => 60, 'maxlength' => 200)); ?>
        <?php echo $form->error($model, 'alias'); ?>
    </div>
</div>
<div class="control-group">
    <?php echo $form->labelEx($model, 'custom_url', array('class' => 'control-label', 'label' => 'Url')); ?>
    <div class="controls">
        <?php echo $form->textField($model, 'custom_url', array('size' => 60, 'maxlength' => 500)); ?>
        <?php echo $form->error($model, 'custom_url'); ?>
    </div>
</div>
<div class="control-group">
    <?php echo $form->labelEx($model, 'bv_review_id', array('class' => 'control-label')); ?>
    <div class="controls">
        <?php echo $form->textField($model, 'bv_review_id', array('size' => 60, 'maxlength' => 100)); ?>
        <?php echo $form->error($model, 'bv_review_id'); ?>
    </div>
</div>
<div style="padding-left: 224px; width: 625px;">
<ul class="nav nav-tabs">
    <li class="active"><a href="#detail1" data-toggle="tab">Detail 1</a></li>
    <li><a href="#detail2" data-toggle="tab">Detail 2</a></li>
    <li><a href="#detail3" data-toggle="tab">Detail 3</a></li>
    <li><a href="#detail4" data-toggle="tab">Detail 4</a></li>
    <li><a href="#detail5" data-toggle="tab">Detail 5</a></li>
</ul>
</div>
<div class="tab-content">
    <div class="tab-pane active" id="detail1">
        <div class="control-group">
            <?php echo $form->labelEx($model, 'details 1', array('class' => 'control-label',)); ?>
            <div class="controls" id="productDetail_1">
                <?php $this->widget('Xpress.extensions.web.widgets.tinymce.ETinyMce', array(
                    'model' => $model,
                    'attribute' => 'details1',
                    'useCompression' => false,
                    'editorTemplate' => 'full',
                    'options' => array(
                        'theme_advanced_buttons1' => 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,undo,redo,|,removeformat,cleanup,|,spellchecker,|,visualaid,visualchars,|,ltr,rtl,|,code,preview,fullscreen'
                    ),
                    'width' => '90%',
                    'height' => '500px',
                )); ?>
                <?php echo $form->error($model, 'details1'); ?>
            </div>
        </div>
    </div>
    <div class="tab-pane" id="detail2">
        <div class="control-group">
            <?php echo $form->labelEx($model, 'details 2', array('class' => 'control-label', 'id' => 'productDetail_2')); ?>
            <div class="controls">
                <!--        --><?php //echo $form->textArea($model, 'details2'); ?>
                <?php $this->widget('Xpress.extensions.web.widgets.tinymce.ETinyMce', array(
                    'model' => $model,
                    'mode' => 'text',
                    'attribute' => 'details2',
                    'useCompression' => false,
                    'editorTemplate' => 'full',
                    'options' => array(
                        'theme_advanced_buttons1' => 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,undo,redo,|,removeformat,cleanup,|,spellchecker,|,visualaid,visualchars,|,ltr,rtl,|,code,preview,fullscreen'
                    ),
                    'width' => '90%',
                    'height' => '250px',
                )); ?>
                <?php echo $form->error($model, 'details2'); ?>
            </div>
        </div>
    </div>
    <div class="tab-pane" id="detail3">
        <div class="control-group">
            <?php echo $form->labelEx($model, 'details 3', array('class' => 'control-label',)); ?>
            <div class="controls">
                <!--        --><?php //echo $form->textArea($model, 'details3'); ?>
                <?php $this->widget('Xpress.extensions.web.widgets.tinymce.ETinyMce', array(
                    'model' => $model,
                    'mode' => 'text',
                    'attribute' => 'details3',
                    'useCompression' => false,
                    'editorTemplate' => 'full',
                    'options' => array(
                        'theme_advanced_buttons1' => 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,undo,redo,|,removeformat,cleanup,|,spellchecker,|,visualaid,visualchars,|,ltr,rtl,|,code,preview,fullscreen'
                    ),
                    'width' => '90%',
                    'height' => '250px',
                )); ?>
                <?php echo $form->error($model, 'details3'); ?>
            </div>
        </div>
    </div>
    <div class="tab-pane" id="detail4">
        <div class="control-group">
            <?php echo $form->labelEx($model, 'details 4', array('class' => 'control-label',)); ?>
            <div class="controls">
                <!--        --><?php //echo $form->textArea($model, 'details4'); ?>
                <?php $this->widget('Xpress.extensions.web.widgets.tinymce.ETinyMce', array(
                    'model' => $model,
                    'mode' => 'text',
                    'attribute' => 'details4',
                    'useCompression' => false,
                    'editorTemplate' => 'full',
                    'options' => array(
                        'theme_advanced_buttons1' => 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,undo,redo,|,removeformat,cleanup,|,spellchecker,|,visualaid,visualchars,|,ltr,rtl,|,code,preview,fullscreen'
                    ),
                    'width' => '90%',
                    'height' => '250px',
                )); ?>
                <?php echo $form->error($model, 'details4'); ?>
            </div>
        </div>
    </div>
    <div class="tab-pane" id="detail5">
        <div class="control-group">
            <?php echo $form->labelEx($model, 'details 5', array('class' => 'control-label',)); ?>
            <div class="controls">
                <!--        --><?php //echo $form->textArea($model, 'details5'); ?>
                <?php $this->widget('Xpress.extensions.web.widgets.tinymce.ETinyMce', array(
                    'model' => $model,
                    'mode' => 'text',
                    'attribute' => 'details5',
                    'useCompression' => false,
                    'editorTemplate' => 'full',
                    'options' => array(
                        'theme_advanced_buttons1' => 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,undo,redo,|,removeformat,cleanup,|,spellchecker,|,visualaid,visualchars,|,ltr,rtl,|,code,preview,fullscreen'
                    ),
                    'width' => '90%',
                    'height' => '250px',
                )); ?>
                <?php echo $form->error($model, 'details5'); ?>
            </div>
        </div>
    </div>
</div>
<!--<div class="control-group">-->
<!--    <div class="controls">-->
<!--        --><?php //echo CHtml::link('More details.', '', array('id' => 'productMoreDetails',)); ?>
<!--    </div>-->
<!--</div>-->
<!--<div id='more_details' style="display: none;width: 100%">-->
<!--</div>-->
<!--<div class="control-group">
    <?php /*$catData = $this->getCategoriesForUpdate($model); */ ?>
    <?php /*echo CHtml::label('Categories', '', array('class' => 'control-label')) */ ?>
    <div class="controls">
        <?php /*echo CHtml::dropDownList('categories[]', $catData['selected'], $catData['data'], array('multiple' => 'multiple', 'style' => 'height:200px')) */ ?>
    </div>
</div>-->
<div class="control-group">
    <?php
    $cats = app()->XService->run('Shop.Category.buildCategoryTree', array(
        'cat_id' => 0,
        'status' => true,
        'category_type_id' => 1,
        'attributes' => array('title')
    ));
    // get selected categories
    $selectedCatIds = array();
    if (!$model->isNewRecord) {
        $selectedCats = $model->categories;
        if (is_array($selectedCats) && count($selectedCats) > 0) {
            foreach ($selectedCats as $selectedCat)
                $selectedCatIds[] = $selectedCat->id;
        }
    }
    ?>
    <?php echo CHtml::label('Categories', '', array('class' => 'control-label')) ?>
    <div class="controls">
        <?php
        echo CHtml::openTag('select', array('name' => 'categories[]', 'id' => 'product-category-ids', 'multiple' => 'multiple', 'style' => 'height: 200px; width: 290px;'));
        if (is_array($cats) && count($cats) > 0) {
            foreach ($cats as $cat_id => $cat_data) {
                $htmlOptions = array('value' => $cat_id);
                if (in_array($cat_id, $selectedCatIds)) $htmlOptions['selected'] = 'selected';
                echo CHtml::tag('option', $htmlOptions, str_repeat('--', $cat_data['level']) . $cat_data['title']);
            }
        }
        echo CHtml::closeTag('select');
        ?>
    </div>
</div>
<div class="control-group">
    <?php echo $form->labelEx($model, 'status', array('class' => 'control-label',)); ?>
    <div class="controls">
        <?php echo $form->checkBox($model, 'status'); ?>
        <?php echo $form->error($model, 'status'); ?>
    </div>
</div>

<div class="control-group">
    <?php echo $form->labelEx($model, 'appearance_settings', array('class' => 'control-label',)); ?>
    <div class="controls">
        <?php
        Yii::import('Shop.controllers.widgets.ProductDetailController');
        $productDetailWidget = new ProductDetailController('widgets/productDetail', app()->getModule('Shop'));
        ?>
        <?php
        echo $form->dropDownList($model, 'appearance_settings', $productDetailWidget->getViews(true));
        ?>
        <?php echo $form->error($model, 'appearance_settings'); ?>
    </div>
</div>

<?php
$this->widget('Shop.components.widgets.DateRange', array(
    'model' => $model,
    'from' => 'available_start_date',
    'to' => 'available_end_date',
    'htmlOptions' => array(
        'class' => 'date-picker date-input',
        'readonly' => 'readonly',
        'style' => 'width: 120px;'
    )
));
?>


<!-- AVAILABLE START/END DATE PICKERS - START -->
<?php
/*$availableStartDatePickerId = 'available_start_date_' . time();
$availableEndDatePickerId = 'available_end_date_' . time();
*/
?><!--
<div class="control-group">
    <?php /*echo $form->labelEx($model, 'available_start_date', array('class' => 'control-label',)); */ ?>
    <div class="controls">
        <?php
/*        $pattern = Yii::app()->locale->getDateFormat('medium');

        $timestamp = strtotime($model->available_start_date);
        if ($timestamp !== false)
            $model->available_start_date = Yii::app()->getDateFormatter()->format($pattern, $timestamp);
        else
            $model->available_start_date = '';

        $this->widget('zii.widgets.jui.CJuiDatePicker', array(
            'id' => $availableStartDatePickerId,
            'model' => $model,
            'attribute' => 'available_start_date',
            // additional javascript options for the date picker plugin
            'options' => array(
                'dateFormat' => 'M d, yy',
                'beforeShow' => 'js:function(input, inst) {
                    $(inst.input).datepicker("option", "minDate", new Date());
                    var maxDate = $("#' . $availableEndDatePickerId . '").val();
                    if (maxDate != "")
                        maxDate = new Date(maxDate);console.log(maxDate);
                    if (maxDate)
                        $(inst.input).datepicker("option", "maxDate", maxDate);
                }'
            ),
            'htmlOptions' => array(
                'class' => 'date-picker available_start_date date-input',
                'readonly' => 'readonly',
                'style' => 'width: 120px;'
            ),
        ));*/
?>
        <?php /*echo $form->error($model, 'available_start_date'); */ ?>
    </div>
</div>
<div class="control-group">
    <?php /*echo $form->labelEx($model, 'available_end_date', array('class' => 'control-label',)); */ ?>
    <div class="controls">
        <?php
/*        $timestamp = strtotime($model->available_end_date);
        if ($timestamp !== false)
            $model->available_end_date = Yii::app()->getDateFormatter()->format($pattern, $timestamp);
        else
            $model->available_end_date = '';
        $datePicker = $this->widget('zii.widgets.jui.CJuiDatePicker', array(
            'id' => $availableEndDatePickerId,
            'model' => $model,
            'attribute' => 'available_end_date',
            // additional javascript options for the date picker plugin
            'options' => array(
                'dateFormat' => 'M d, yy',
                'beforeShow' => 'js:function(input, inst) {
                    var minDate = $("#' . $availableStartDatePickerId . '").val();
                    if (minDate != "")
                        minDate = new Date(minDate);
                    if (!minDate)
                        minDate = new Date();
                    $(inst.input).datepicker("option", "minDate", minDate);
                }'
            ),
            'htmlOptions' => array(
                'class' => 'date-picker available_end_date date-input',
                'readonly' => 'readonly',
                'style' => 'width: 120px;'
            ),
        ));
        app()->clientScript->registerCssFile($datePicker->themeUrl . '/' . $datePicker->theme . '/' . $datePicker->cssFile);
        */
?>
        <?php /*echo $form->error($model, 'available_end_date');*/ ?>
    </div>
</div>-->
<!-- AVAILABLE START/END DATE PICKERS - END -->

<div class="control-group">
    <?php echo $form->labelEx($model, 'on_sale', array('class' => 'control-label',)); ?>
    <div class="controls">
        <?php echo $form->checkBox($model, 'on_sale'); ?>
        <?php echo $form->error($model, 'on_sale'); ?>
    </div>
</div>
<div class="on_sale-input hide">
    <?php
    $this->widget('Shop.components.widgets.DateRange', array(
        'model' => $model,
        'from' => 'on_sale_start',
        'to' => 'on_sale_end',
        'htmlOptions' => array(
            'class' => 'date-picker date-input',
            'readonly' => 'readonly',
            'style' => 'width: 120px;'
        )
    ));
    ?>
</div>
<?php
app()->clientScript->registerCoreScript('jquery.ui');
app()->clientScript->registerScript('on-sale', '
$("#product-form #' . CHtml::activeId($model, 'on_sale') . '").unbind("change");
$("#product-form #' . CHtml::activeId($model, 'on_sale') . '").bind("change",function(e){
    if($(this).is(":checked")){
        $(".on_sale-input").removeClass("hide");
    }else{
        $(".on_sale-input").addClass("hide");
    }
});
if ($("#product-form #' . CHtml::activeId($model, 'on_sale') . '").is(":checked"))
{
    $(".on_sale-input").removeClass("hide");
}
', CClientScript::POS_READY) ?>
<!--<script type="application/javascript">-->
<!--    $('#productMoreDetails').click(function () {-->
<!--        $('#more_details').toggle();-->
<!--    });-->
<!--    $("#productMoreDetails").hover(-->
<!--        function () {-->
<!--            $(this).css("color", "blue");-->
<!--            $(this).css('cursor', 'pointer');-->
<!--        }, function () {-->
<!--            $(this).css("color", "#F26522");-->
<!--            $(this).css('cursor', 'auto');-->
<!--        }-->
<!--    );-->
<!--</script>-->